<?php
namespace app\index\logic;
use think\Db;

class Novel
{

    /*
     * 获取列表数据以ID为键名
     * */
    public function getListById($nids)
    {
        $where[] = ['id', 'in', $nids];
        $where[] = ['in_shelf', '=', 1];

        $NovelModel = \think\facade\App::model('Novel', 'model');
        $list = $NovelModel->getList($where, 'id,origin_title,cover,brief,update_status,class_id', 1, count($nids));
        if ($list) {

            $cat_id_gh = array_unique(array_column($list, 'class_id'));
            $cats = Db::table('novel_class')->where('id','in',$cat_id_gh)->column('id,name');

            foreach ($list as $k => $v) {
                $list[$k]['cover']          = strlen($v['cover']) > 1 ? config('img_domain').$v['cover'] : '';
                $list[$k]['update_status']  = getByUpdateStatusName($v['update_status']);
                $list[$k]['cat_name']       = isset($cats[$v['class_id']]) ? $cats[$v['class_id']] : '';
                $list[$k]['bookname']       = $v['origin_title'];
                $list[$k]['brief']          = $v['brief'];
            }
        }

        return $list ? array_column($list, null, 'id') : [];
    }

    /*
     * 书籍搜索列表获取
     * */
    public function getSearchList($params)
    {
        $params['page']     = isset($params['Pages']) ? $params['Pages'] : 1;
        $params['limit']    = isset($params['Size']) ? $params['Size'] : 20;

        $params['freq_id']    = isset($params['freq_id']) ? $params['freq_id'] : '';
        $params['class_id']    = isset($params['class_id']) ? $params['class_id'] : '';
        $params['son_class_id']    = isset($params['son_class_id']) ? $params['son_class_id'] : '';
        $params['update_status']    = isset($params['update_status']) ? $params['update_status'] : '';

        $params['in_shelf'] = 1;

        $condition[] = ['origin_title', 'like', 'require','keyword'];
        $condition[] = ['in_shelf', 'eq', 'gt:0','in_shelf'];
        $condition[] = ['freq_id', 'eq', 'require','freq_id'];
        $condition[] = ['class_id', 'eq', 'require','class_id'];
        $condition[] = ['son_class_id', 'eq', 'require','son_class_id'];
        $condition[] = ['update_status','eq','require','update_status'];

        $where   = buildQueryCondition($condition, $params);

        $checkIpBlackResult = checkIpBlackByToken();
        if($checkIpBlackResult['status'] == true)
        {
            $where[] = ['scale','=','C'];
        }else{
            $scale_grade_gh = getScaleGh(request()->scale_grade);
            $where[] = ['scale','in',$scale_grade_gh];
        }

        $orderBy = [ 'update_time'=>'DESC', 'id' => 'DESC'];

        $NovelModel = \think\facade\App::model('Novel', 'model');
        $list = $NovelModel->getList($where, 'id,origin_title,cover,brief,update_status,class_id', $params['page'], $params['limit'], $orderBy );

        $data = [];

        if ($list) {

            $cat_id_gh = array_unique(array_column($list, 'class_id'));
            $cats = Db::table('novel_class')->where('id','in',$cat_id_gh)->column('id,name,en_name');

            foreach ($list as $k => $v) {
                $data[$k]['cover']          = strlen($v['cover']) > 1 ? config('img_domain').$v['cover'] : '';
                $data[$k]['update_status']  = getByUpdateStatusName($v['update_status']);
				if($params['lang'] == 'en'){
					$data[$k]['cat_name']       = isset($cats[$v['class_id']]) ? $cats[$v['class_id']]['en_name'] : '';
				}else{
					$data[$k]['cat_name']       = isset($cats[$v['class_id']]) ? $cats[$v['class_id']]['name'] : '';
				}
                
                $data[$k]['bookname']       = $v['origin_title'];
                $data[$k]['brief']          = $v['brief'];
                $data[$k]['nid']            = $v['id'];

            }
        }

        $result['total']    = $NovelModel->getTotalNumber($where);
        $result['list']     = $data;

        return $result;
    }

    /*
     * 书籍详情
     * */
    public function getInfo($params)
    {
        $where[] = ['id','=',$params['nid']];
        $where[] = ['in_shelf','=',1];

        $field = "id,origin_title,author,cover,update_status,number_words,brief,chapter_up_time,class_id";

        $NovelModel = \think\facade\App::model('Novel', 'model');
        $record = $NovelModel->getOne($where,$field);

        $info = [];
        if($record)
        {
            $chapterMax = Db::table('novel_chapter')->where('novel_id',$record['id'])->order('chapter_no DESC')
                        ->field('chapter_no,create_time')->findOrEmpty();

            $category = Db::table('novel_class')->where('id','=',$record['class_id'])->field('id,name')->findOrEmpty();

            $info['nid']                    = $record['id'];
            $info['bookname']               = $record['origin_title'];
            $info['author']                 = $record['author'];
            $info['vertical_cover']         = strlen($record['cover']) > 1 ? config('img_domain').$record['cover'] : '';
            $info['update_status']          = getByUpdateStatusName($record['update_status']);
            $info['brief']                  = $record['brief'];
            $info['chapter_recent_date']    = $chapterMax['create_time'];
            $info['chapter_recent']         = '第'.$chapterMax['chapter_no'].'章';
            $info['cat_name']               = $category ? $category['name'] : '';
            $info['word_number']            = numerical_conversion($record['number_words']);

        }

        return $info;
    }

    /*
     * 获取同类型相关书籍
     * */
    public function getRelatedBooks($nid)
    {
        $class_id = Db::table('novel')->where('id',$nid)->value('class_id');
        $where[] = ['class_id','=',$class_id];
        $where[] = ['in_shelf','=',1];
        $where[] = ['id','<>',$nid];

        $checkIpBlackResult = checkIpBlackByToken();
        if($checkIpBlackResult['status'] == true)
        {
            $where[] = ['scale','=','C'];
        }else{
            $scale_grade_gh = getScaleGh(request()->scale_grade);
            $where[] = ['scale','in',$scale_grade_gh];
        }

        $list = Db::table('novel')->where($where)
                ->orderRaw('rand()')->limit(4)->field('id,origin_title,cover')->select();

        $data = [];
        if($list)
        {
            foreach ($list as $k=>$v)
            {
                $data[$k]['nid']            = $v['id'];
                $data[$k]['bookname']       = $v['origin_title'];
                $data[$k]['vertical_cover'] = strlen($v['cover']) > 1 ? config('img_domain').$v['cover'] : '';
            }
        }

        return $data;
    }

}